package com.dingding.special.user.system.service.impl;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.code.BaseCodeMessage;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.dto.AbstractValiResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.Versioninfo;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.bean.BeanUtilx;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.special.user.system.dto.ReqCheckUpdateDTO;
import com.dingding.special.user.system.dto.ResCheckUpdateDTO;
import com.dingding.special.user.system.service.ICheckUpdateService;

/**
 * 
 * @ClassName: ICheckUpdate 
 * @Description: 版本检查更新
 * @author CHEHAUNBO 
 * @date 2016年8月30日 下午3:09:12
 * 
 */
@Component("checkUpdateService")
public class CheckUpdateImpl extends AbstractValiResponse implements ICheckUpdateService {

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	private static Logger log = Logger.getLogger(CheckUpdateImpl.class);

	/**
	 * 
	 * 检查更新
	 * 
	 * @Title: checkUpdate 
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public ResponseEntity<ResCheckUpdateDTO> checkUpdate(ReqCheckUpdateDTO reqCheckUpdateDTO) {

		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(reqCheckUpdateDTO, ReqCheckUpdateDTO.class);

		if (!valiResult.isAvailable()) {
			return super.error(valiResult);
		}

		try {

			if (ToolUtil.isNotBlank(reqCheckUpdateDTO.getUserId())) {
				Criterion criterion = Restrictions.eq("suid", reqCheckUpdateDTO.getUserId());
				Specialuserlogin specialuserlogin = criteriaBaseDao.queryCriteriaListUnique(Specialuserlogin.class,
						criterion);
				if (specialuserlogin != null
						&& !reqCheckUpdateDTO.getClientVersion().equals(specialuserlogin.getClientVersion())) {
					specialuserlogin.setClientVersion(reqCheckUpdateDTO.getClientVersion());
				}
			}

			Criterion criterion = null;
			if (reqCheckUpdateDTO.getDeviceType() == 3) { // iOS
				criterion = Restrictions.eq("clientType", 3);
			} else { // Android
				criterion = Restrictions.eq("clientType", 4);
			}

			Versioninfo versioninfo = criteriaBaseDao.queryCriteriaListUnique(Versioninfo.class, criterion);

			if(versioninfo != null){
				
				ResCheckUpdateDTO resCheckUpdateDTO = new ResCheckUpdateDTO();
				BeanUtilx.copyProperties(resCheckUpdateDTO, versioninfo);
				
				return super.success(resCheckUpdateDTO, HttpStatus.SC_OK);
			}

		} catch (Exception e) {
			log.error("检查更新出现异常！", e);
		}
		
		return super.error(HttpStatus.SC_INTERNAL_SERVER_ERROR, BaseCodeMessage.SERVER_ERROR,
				BaseCodeMessage.SERVER_ERROR);

	}

}
